//
//  LetterView.m
//  XinKaiFa55like
//
//  Created by 55like on 2017/9/25.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "LetterView.h"

@implementation LetterView
{
    NSArray * titleArray;
    NSMutableArray * buttonArray;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        buttonArray = [[NSMutableArray alloc]init];
        titleArray = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"];
        
        for (int i = 0; i < titleArray.count; i++) {
            NSString * str = titleArray[i];
            UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, i * frame.size.height/titleArray.count, frame.size.height/titleArray.count, frame.size.height/titleArray.count)];
            button.frameWidth = button.frameWidth>[str widthWithFont:14]?button.frameWidth:(frame.size.height/titleArray.count);
            [button setTitle:titleArray[i] forState:UIControlStateNormal];
            [button setTitle:titleArray[i] forState:UIControlStateSelected];
            [button setTitleColor:rgbTxtGray forState:UIControlStateNormal];
            [button setTitleColor:rgbpublicColor forState:UIControlStateSelected];
            button.titleLabel.font = Font(14);
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
            [buttonArray addObject:button];
           
        }
        
        
        
    }
    return self;
}
-(void)buttonClick:(UIButton *)button{
    

    for (int i = 0; i < buttonArray.count; i ++) {
        
        UIButton * btn = buttonArray[i];
        
        btn.selected = NO;
    
        
    }
    
    button.selected = YES;
    
    if ([self.delegate respondsToSelector:@selector(letterViewDidTitle:)]) {
        
        
        [self.delegate letterViewDidTitle:button.titleLabel.text];
        
        
    }
}

@end
